<template>
  <transition
    name="fade-transform"
    mode="out-in"
    @after-enter="afterEnter"
  >>
    <div v-show="dialogVisible" class="lic-info">
      <div class="header">
        <div class="header-title">{{ title }}</div>
        <i
          class="header-close el-icon-circle-close"
          @click="afterLeave"
        />
      </div>
      <div class="content">
        <slot name="content" />
        <!-- 按钮插槽 -->
        <div v-if="tools" slot="footer" class="footer dialog-footer">
          <slot name="tools">
            <el-button type="primary" :disabled="disabled" @click="ok">{{ okBtn }}</el-button>
            <el-button @click="afterLeave">{{ cancelBtn }}</el-button>
          </slot>
        </div>
      </div>
    </div>
  </transition>
</template>

<script>
export default {
  name: 'MsInfo',
  props: {
    title: {
      type: String,
      default: ''
    },
    dialogVisible: {
      type: Boolean,
      default: true
    },
    tools: {
      type: Boolean,
      default: true
    },
    cancelBtn: {
      type: String,
      default: '返回'
    },
    okBtn: {
      type: String,
      default: '确定'
    }
  },
  data() {
    return {
      disabled: false
    }
  },
  methods: {
    ok() {
      this.disabled = true
      this.$emit('ok')
    },
    afterEnter() {
      this.handleResetButton()
      this.$emit('show')
    },
    afterLeave() {
      // 保证每一次进入表单 置顶
      this.$nextTick(() => {
        const infoBox = document.getElementsByClassName('content')[0]
        if (infoBox) {
          infoBox.scrollTop = 0
          infoBox.scrollLeft = 0
        }
      })
      this.$emit('close')
    },
    handleResetButton() {
      this.disabled = false
    }
  }
}
</script>

<style lang="stylus" scoped>
  $boxHeight = 10px
  .lic-info {
    position: absolute;
    overflow: hidden;
    padding 0 20px;
    width: 100%;
    box-sizing: border-box;
    height: 100%;
    border: 1px solid #eee;
    border-radius: 4px;
    .header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      height: 40px;
      line-height: 40px;
      padding: 0 $boxHeight;
      border-bottom: 1px solid #eee;
      box-sizing: border-box;
      &-title {
        font-size: 15px;
        font-weight: 600;
        letter-spacing: 0.04em;
        line-height: 16px;
      }
      &-close {
        cursor: pointer;
        font-size: 20px;
      }
    }
    .content {
      overflow: auto;
      /** 减去头部高度 */
      height calc(100% - 40px - (3 * 10px))
      padding: 0 10px 10px 0;
      margin-top: $boxHeight;
      box-sizing: border-box;
      .footer {
        text-align: center;
      }
    }
  }
</style>
